package com.tiyende.kash.loop.utils.helper

import com.tiyende.kash.loop.BuildConfig
import com.tiyende.kash.loop.TiyendeApp
import com.tiyende.kash.loop.configs.TiyendeApiPath
import com.tiyende.kash.loop.configs.TiyendeApiUrl
import com.tiyende.kash.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.kash.loop.configs.TiyendeConfigs
import com.tiyende.kash.loop.request.HttpHelper
import com.tiyende.kash.loop.request.IHttpCallback
import com.tiyende.kash.loop.sdk.analyse.AnalyseBuilder
import com.tiyende.kash.loop.sdk.analyse.AnalyseUtil
import com.tiyende.kash.loop.sdk.dec.TiyendeUserDecAsy
import com.tiyende.kash.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.kash.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject

/**
 * TODO: Analysis Lib 初始化时机
 */
object TiyendeAnalysisHelper {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalyseBuilder.product = TiyendeConfigs.TIYENDE_APPNAME
        AnalyseBuilder.appVersion = BuildConfig.VERSION_CODE.toString()
        AnalyseBuilder.appPckName = TiyendeConfigs.TIYENDE_PACKAGENANME

        AnalyseBuilder.aesKey = TiyendeConfigs.getSecretKey()
        AnalyseBuilder.aesIv = TiyendeConfigs.getSecretIv()

        AnalyseBuilder.host = TiyendeApiUrl.TIYENDE_HOST
        AnalyseBuilder.zone = ""
        AnalyseBuilder.pathCommon = TiyendeApiUrl.getRandomUrl()

        AnalyseBuilder.pathGoogleInstall = TiyendeApiPath.TIYENDE_ANALYSIS.TIYENDE_ANALYSIS_GOOGLE_INSTALL_PATH
        AnalyseBuilder.pathAdjustInstall = TiyendeApiPath.TIYENDE_ANALYSIS.TIYENDE_ANALYSIS_ADJUST_INSTALL_PATH
        AnalyseBuilder.pathFacebookInstall = TiyendeApiPath.TIYENDE_ANALYSIS.TIYENDE_ANALYSIS_FACEBOOK_INSTALL_PATH
        AnalyseBuilder.pathFirebaseInstall = TiyendeApiPath.TIYENDE_ANALYSIS.TIYENDE_ANALYSIS_FIREBASE_INSTALL_PATH

        AnalyseUtil.init(TiyendeApp.app)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalyseBuilder.zone = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_TIMEZONE)
        AnalyseUtil.init(TiyendeApp.app)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalyseBuilder.token = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_TOKEN)
        AnalyseBuilder.userId = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_USERID)
        AnalyseUtil.init(TiyendeApp.app)
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TiyendeUserDecInvok {

    /**
     * 获取设备数据上传状态
     */
    fun queryTiyendeDeviceInfoTypesAction(){
        val params = JSONObject().addCommonParams().apply {
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_DEVICE_INFO_TYPES_PATH)
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            val deviceInfoTypes = obj.optString("deviceInfoTypes")
                            TiyendeUserDecAsy.startUserDecTasks(deviceInfoTypes)
                        }
                    }
                    else -> {//失败全部上报
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}